<template>
  <el-container class="home-container">
    <!--头部-->
    <el-header>
      <div class="text">连锁酒店预订平台</div>
      <div class="avatar_group">
        <el-avatar shape="square">{{ this.user.username }}</el-avatar>
        <el-button type="danger" plain @click="logout">退出</el-button>
      </div>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '200px' ">
        <div class="toggle-button" @click="toggleCollapse">|||
        </div>
        <el-menu
            class="el-menu-vertical-demo"
            background-color="#313743"
            text-color="#fff"
            active-text-color="#1d50b4"
            unique-opened
            :collapse="isCollapse"
            :collapse-transition="false"
            router
            :default-active="activePath">
          <!--一级菜单-->
          <el-submenu index="/user">
            <!--一级菜单模板区-->
            <template slot="title">
              <i class="el-icon-user"></i>
              <span>用户</span>
            </template>
            <!--二级菜单-->
            <el-menu-item index="/welcome" @click="saveNavStatus('/welcome')">
              <template slot="title">
                <i class="el-icon-s-shop"></i>
                <span>首页</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/myOrder" @click="saveNavStatus('/myOrder')">
              <template slot="title">
                <i class="el-icon-s-claim"></i>
                <span>我的订单</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/editInfo" @click="saveNavStatus('/editInfo')">
              <template >
                <i class="el-icon-setting"></i>
                <span>个人信息</span>
              </template>
            </el-menu-item>

          </el-submenu>
          <!--一级菜单-->
          <el-submenu index="/admin" :disabled="!isAdmin">
            <!--一级菜单模板区-->
            <template slot="title">
              <i class="el-icon-s-management"></i>
              <span>管理</span>
            </template>
            <!--二级菜单-->
            <el-menu-item index="/userTable" @click="saveNavStatus('/userTable')">
              <template slot="title">
                <i class="el-icon-s-custom"></i>
                <span>用户列表</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/hotelTable" @click="saveNavStatus('/hotelTable')">
              <template slot="title">
                <i class="el-icon-office-building"></i>
                <span>酒店列表</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/roomTable" @click="saveNavStatus('/hotelTable')">
              <template slot="title">
                <i class="el-icon-s-home"></i>
                <span>房间列表</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/orderTable" @click="saveNavStatus('/orderTable')">
              <template slot="title">
                <i class="el-icon-s-order"></i>
                <span>订单管理</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/logs" @click="saveNavStatus('/logs')" :disabled="!isSuperAdmin">
              <template slot="title">
                <i class="el-icon-notebook-2"></i>
                <span>日志</span>
              </template>
            </el-menu-item>
          </el-submenu>

        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>


<script>
import axios from "axios";

export default {
  name: 'home',
  data() {
    return {
      isCollapse: false,
      activePath: '',
      user: '',
      userRole:'',
    }
  },
  created() {
    this.activePath = window.sessionStorage.getItem('activePath');
    const userJson = window.sessionStorage.getItem('user');
    this.user = JSON.parse(userJson);
    this.userRole=this.user.userRole
  },
  methods: {
    //退出登录
    logout() {
      axios.post("/api/user/logout").then(() => {
        this.$message.success('退出成功')
        window.sessionStorage.removeItem('user')
        window.sessionStorage.removeItem('activePath');
        this.$router.push('/login')
      })
    },
    //水平展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    //保存连接激活状态
    saveNavStatus(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
    },

  },
  computed: {
    isAdmin() {
      return (this.userRole === 1) || (this.userRole === 2);
    },
    isSuperAdmin(){
      return this.userRole === 2;
    }
  }
}
</script>

<style scoped>
.home-container {
  height: 100%;
}

.el-header {
  background-color: #001529;
  display: flex;
  justify-content: space-between;
  align-items: center
}

.el-aside {
  background-color: #313743;

  .el-menu {
    border-right: none
  }
}

.el-main {
  background-color: #fff;
}

.text {
  color: white;
  font-size: 25px;
}

.toggle-button {
  background-color: #4A5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

.avatar_group {
  display: flex;
  justify-content: space-between;
  align-items: center
}
</style>